package com.issue.system.controller;

import com.issue.common.annotation.Log;
import com.issue.common.core.controller.BaseController;
import com.issue.common.core.domain.AjaxResult;
import com.issue.common.core.domain.entity.SysUser;
import com.issue.common.core.page.TableDataInfo;
import com.issue.common.enums.BusinessType;
import com.issue.common.utils.poi.ExcelUtil;
import com.issue.system.domain.AChildInfo;
import com.issue.system.domain.ChildAssignment;
import com.issue.system.domain.ChildCourse;
import com.issue.system.mapper.AChildInfoMapper;
import com.issue.system.mapper.ChildAssignmentMapper;
import com.issue.system.mapper.ChildCourseMapper;
import com.issue.system.mapper.SysUserMapper;
import com.issue.system.service.IChildAssignmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * AssignmentController
 *
 * @author system
 * @date 2024-10-20
 */
@RestController
@RequestMapping("/system/assignment")
public class ChildAssignmentController extends BaseController {

    @Autowired
    private IChildAssignmentService childAssignmentService;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private AChildInfoMapper childInfoMapper;

    @Resource
    private ChildAssignmentMapper childAssignmentMapper;

    @Resource
    private ChildCourseMapper childCourseMapper;

    /**
     * 查询Assignment列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ChildAssignment childAssignment) {
        startPage();
        List<ChildAssignment> list = childAssignmentService.selectChildAssignmentList(childAssignment);
        return getDataTable(list);
    }

    /**
     * 查询Assignment列表
     */
    @GetMapping("/listAll")
    public TableDataInfo listAll(@RequestParam("phone") String phone,
                                 @RequestParam(value = "courseId", required = false) Long courseId,
                                 @RequestParam(value = "childId", required = false) Long childId) {
        List<ChildAssignment> list = childAssignmentMapper.selectListByPhone(phone, courseId, childId);
        return getDataTable(list);
    }

    /**
     * 导出Assignment列表
     */
    @PreAuthorize("@ss.hasPermi('system:assignment:export')")
    @Log(title = "Assignment", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChildAssignment childAssignment) {
        List<ChildAssignment> list = childAssignmentService.selectChildAssignmentList(childAssignment);
        ExcelUtil<ChildAssignment> util = new ExcelUtil<ChildAssignment>(ChildAssignment.class);
        util.exportExcel(response, list, "Assignment数据");
    }

    /**
     * 获取Assignment详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:assignment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(childAssignmentService.selectChildAssignmentById(id));
    }

    /**
     * 新增Assignment
     */
    @Log(title = "Assignment", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChildAssignment childAssignment) {

        Long teacherId = childAssignment.getTeacherId();
        Long childId = childAssignment.getChildId();
        Long courseId = childAssignment.getCourseId();

        if (teacherId == null || childId == null) {
            return AjaxResult.error("Error Message");
        }

        ChildCourse childCourse = childCourseMapper.selectChildCourseByCourseId(courseId);
        if (childCourse == null){
            return AjaxResult.error("Error Course");
        }

        SysUser teacher = sysUserMapper.selectUserById(teacherId);
        if (teacher == null){
            return AjaxResult.error("Error Teacher");
        }
        childAssignment.setTeacherName(teacher.getUserName());

        AChildInfo childInfo = childInfoMapper.selectAChildInfoByChildId(childId);
        if (childInfo == null){
            return AjaxResult.error("Error Child");
        }
        childAssignment.setChildCardNo(childInfo.getChildCardNo());
        childAssignment.setChildName(childInfo.getChildName());
        childAssignment.setCourseName(childCourse.getCourseName());
        childAssignment.setCourseId(childCourse.getCourseId());

        return toAjax(childAssignmentService.insertChildAssignment(childAssignment));
    }

    /**
     * 修改Assignment
     */
    @PreAuthorize("@ss.hasPermi('system:assignment:edit')")
    @Log(title = "Assignment", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChildAssignment childAssignment) {

        Long teacherId = childAssignment.getTeacherId();
        Long childId = childAssignment.getChildId();

        if (teacherId != null) {
            SysUser teacher = sysUserMapper.selectUserById(teacherId);
            if (teacher != null){
                childAssignment.setTeacherName(teacher.getUserName());
            }
        }

        if (childId != null) {
            AChildInfo childInfo = childInfoMapper.selectAChildInfoByChildId(childId);
            if (childInfo != null){
                childAssignment.setChildCardNo(childInfo.getChildCardNo());
                childAssignment.setChildName(childInfo.getChildName());
            }
        }

        return toAjax(childAssignmentService.updateChildAssignment(childAssignment));
    }

    /**
     * 删除Assignment
     */
    @PreAuthorize("@ss.hasPermi('system:assignment:remove')")
    @Log(title = "Assignment", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(childAssignmentService.deleteChildAssignmentByIds(ids));
    }
}
